package com.dvd.cjc.gcjc001;
import java.util.ArrayList;
import java.util.List;

import com.dvd.common.CommonAction;
import com.dvd.dao.usr.*;
import com.dvd.utils.Message;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import java.text.*;
import com.dvd.utils.*;
public class GCJC001Action extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CommonAction.New(request,this.servlet);
		String action = request.getParameter("action");
		if(action.length() > 0)
		{
			action = "edit";
		}
		else
		{
			action="new";
		}
		if(action.equals("new"))
		{
			return this.entryAdmin("new",mapping, form, request, response);
		}
		else if(action.equals("edit"))
		{
			return this.entryAdmin("edit",mapping, form, request, response);
		}
		else
		{
			assert("INVALID ACTION" == null);
			return null;
		}
	}
	public ActionForward entryAdmin(String flag,ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		
		/*Full the TUsr*/
		GCJC001Bean bean = (GCJC001Bean)form;
		TUsr usr = new TUsr();
		usr.setFLoginId( bean.getFLoginId());
		usr.setFUsrNm(bean.getFUsrNm());
		usr.setFPwd(bean.getFPwd());
		usr.setFBirth(DateMaker.getInstance().parse(bean.getFBirth()));
		System.out.println(usr.getFBirth());
		usr.setFMail(bean.getFMail());
		
		
		/*update database*/
		GCJC001Model model = new GCJC001Model();
		boolean result = model.entryAdmin(flag,usr);
		if(result == true)//new 
		{
			servlet.getServletContext().setAttribute("registerState", "true");
			String message;
			if(flag.equals("new"))
			{
				return mapping.findForward("GCJC002");
			}
			else//edit
			{
				request.setAttribute("TUsr", usr);
				List<Message> messages =  new ArrayList<Message>();
				messages.add(new Message("","INFO","DVD情報は成功に更新しました！"));
				request.setAttribute("List<Message>",messages);
				return mapping.getInputForward();
			}
		}
		return null;
	}
	

}
